package com.iqik.sc.ns.service.impl;

import com.iqik.sc.dao.UserDao;
import com.iqik.sc.pojo.User;
import com.iqik.sc.ns.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public boolean hadUser(String email) {
        User param = new User();
        param.setEmail(email);
        Optional<User> result = userDao.findOne(Example.of(param));
        return result.isPresent();
    }

    @Override
    public User registerUser(String email, String password) {
        try {
            User user = new User();
            user.setEmail(email);
            user.setPassword(password);
            return userDao.save(user);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public User login(String email, String password) {
        User user = new User();
        user.setPassword(password);
        user.setEmail(email);
        Optional<User> result = userDao.findOne(Example.of(user));
        return result.isEmpty() ? null : result.get();
    }

}
